/*
 *  Project     : QKeyboardStatistic
 *  Author      : Durachenko Aleksey V. <durachenko.aleksey@gmail.com>, 2011
 *  Licence     : GNU GENERAL PUBLIC LICENSE Version 2
 */
#include "csettingsdialog.h"

#include <QMessageBox>
#include <QFileInfo>
#include <QFileDialog>
#include "global.h"

// ----------------------------------------------------------------------
CSettingsDialog::CSettingsDialog(QWidget * parent, Qt::WFlags f) : QDialog(parent, f)
{
    // init gui
    setupUi(this);

    //
    G_SETTINGS_INIT();

    //
    lineEdit_Database->setText(settings.value("Database").toString());
    doubleSpinBox_PixelResolution->setValue(settings.value("PixelResolution").toDouble());
    spinBox_AutoSave->setValue(settings.value("AutoSavePeriod").toInt());
    checkBox_SpyKeyboard->setChecked(settings.value("SpyKeyboard").toBool());
    checkBox_SpyMouse->setChecked(settings.value("SpyMouse").toBool());
}


// ----------------------------------------------------------------------
void CSettingsDialog::accept()
{
    G_SETTINGS_INIT();
    settings.setValue("Database", lineEdit_Database->text());
    settings.setValue("PixelResolution", doubleSpinBox_PixelResolution->value());
    settings.setValue("AutoSavePeriod", spinBox_AutoSave->value());
    settings.setValue("SpyKeyboard", checkBox_SpyKeyboard->isChecked());
    settings.setValue("SpyMouse", checkBox_SpyMouse->isChecked());
    QMessageBox::information(this, tr("Note"), tr("Some changes has been accepted after restart application"));
    QDialog::accept();
}


// ----------------------------------------------------------------------
void CSettingsDialog::on_toolButton_OpenDatabase_clicked()
{
    G_SETTINGS_INIT();

    //
    QString baseDir = QFileInfo(settings.value("Database").toString()).absolutePath();

    //
    QString fileName = QFileDialog::getSaveFileName(this,
        tr("Create or open database"), baseDir, tr("Database (* *.*)"));

    //
    if (!fileName.isEmpty())
    {
        lineEdit_Database->setText(fileName);
    }
}
